#############################################################################

library(data.table)
library(TitanCNA)
library(GenomicRanges)
library(argparser)

#############################################################################

argp <- arg_parser("Different ways of handling SNVs not adjusted by CHAT and SNVs in A1 or A2 lineage")
argp <- add_argument(argp, "--work_path", help="work_path")
argp <- add_argument(argp, "--id", help="Sample")
argp <- add_argument(argp, "--phi", help="phi")
argp <- add_argument(argp, "--numClustersStr", help="tnumClustersStr")
argp <- add_argument(argp, "--out_path", help="Output path")
argp <- add_argument(argp, "--ploidy", help="ploidy")
argp <- add_argument(argp, "--purity", help="Puriy")

argv <- parse_args(argp)

if(1!=1){
	id <- "WGC079294"
	work_path <- "/public/user/xxf/20210520_LCSomatic/titan/hmm/WGC079294_titanCNA_ploidy2"
	out_path <- "/public/user/xxf/20210520_LCSomatic/titan/CNAPlot/WGC079294_titanCNA_ploidy2/WGC079294_cluster2"
	phi <- 2 
	numClustersStr <- 2
	ploidy <- 2.219
	norm <- 1-0.3283
}

id <- argv$id
work_path <- argv$work_path
out_path <- argv$out_path
phi <- argv$phi
numClustersStr <- argv$numClustersStr
ploidy <- as.numeric(argv$ploidy)
norm <- 1- as.numeric(argv$purity)

#############################################################################

input_results <- paste0(work_path, "/", id, "_cluster", numClustersStr, ".txt")
input_seg <- paste0(work_path, "/", id, "_cluster", numClustersStr, ".segs.txt")
outplot <- out_path

#############################################################################

results <- fread(input_results)
segs <- fread(input_seg)

#############################################################################

chrs <- paste0( "chr" , c(1:22, 'X'))
plotYlim <- c(-2,4)
numClusters <- 2

#############################################################################

################################################
############## GENOME WIDE PLOTS ###############
################################################
outFile <- paste0(outplot, "/", id, "_cluster", numClustersStr, "_CNA.pdf")
#png(outFile,width=1000,height=300)
pdf(outFile,width=20,height=6)
plotCNlogRByChr(dataIn=results, chr=chrs, segs = segs, ploidy=ploidy,
                normal = norm, geneAnnot=NULL, spacing=4, main=id, xlab="",
                ylim=plotYlim, cex=0.5, cex.axis=1.5, cex.lab=1.5, cex.main=1.5)
dev.off()

outFile <- paste0(outplot, "/", id, "_cluster", numClustersStr, "_CNASEG.pdf")
#png(outFile,width=1000,height=300)
pdf(outFile,width=20,height=6)
maxCorCN <- max(segs$Corrected_Copy_Number, na.rm = TRUE)
plotSegmentMedians(dataIn=segs, chr=chrs, resultType = "LogRatio", plotType = "CopyNumber", 
				plot.new=T, ylim=c(0,maxCorCN), cex.axis=1.5, cex.lab=1.5, cex.main=1.5)
dev.off()

outFile <- paste0(outplot, "/", id, "_cluster", numClustersStr, "_LOH.pdf")
#png(outFile,width=1000,height=300)
pdf(outFile,width=20,height=6)
plotAllelicRatio(dataIn=results, chr=chrs, geneAnnot=NULL, spacing=4,
                 main=id, xlab="", ylim=c(0,1), cex=0.5, cex.axis=1.5,
                 cex.lab=1.5, cex.main=1.5)
dev.off()

outFile <- paste0(outplot, "/", id, "_cluster", numClustersStr, "_LOHSEG.pdf")
#png(outFile,width=1000,height=300)
pdf(outFile,width=20,height=6)
maxCorCN <- max(segs$Corrected_Copy_Number, na.rm = TRUE)
plotSegmentMedians(dataIn=segs, chr=chrs, resultType = "AllelicRatio", plotType = "CopyNumber", 
				plot.new=T, ylim=c(0,maxCorCN), cex.axis=1.5, cex.lab=1.5, cex.main=1.5)
dev.off()

outFile <- paste0(outplot, "/", id, "_cluster", numClustersStr, "_CF.pdf")
#png(outFile,width=1000,height=300)
pdf(outFile,width=20,height=6)
plotClonalFrequency(dataIn=results, chr=chrs, norm, geneAnnot=NULL,
                    spacing=4, main=id, xlab="", ylim=c(0,1), cex.axis=1.5,
                    cex.lab=1.5, cex.main=1.5)
dev.off()
